package map;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.*;


public class MapThreadChatServer {
    // 定义服务端的ServerSocket属性
    private ServerSocket serverSocket;
    private Map<String ,PrintWriter> writerList = new HashMap<>();
    private String host;
    private String userName;

    public MapThreadChatServer(){
        try {
            System.out.println("服务器启动");
            serverSocket = new ServerSocket(8080);
            System.out.println("服务器启动OK");

        }catch (IOException e){
            System.out.println("服务器启动失败");
            e.printStackTrace();
            System.exit(1);
        }

    }

    private class ClientHandle implements Runnable{
        private Socket socket;

        //    private int host;

        public ClientHandle(Socket socket){
            this.socket=socket;
            host=this.socket.getInetAddress().getHostAddress();
//        host=socket.getPort();
        }
        @Override
        public void run() {
            PrintWriter pw = null;
            try {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));

                pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(socket.getOutputStream(),
                                        StandardCharsets.UTF_8)),true);

                userName = br.readLine();
                synchronized (writerList) {
                    writerList.put(userName, pw);
                }
                sendMessageToAllClient(userName+"上线了");

                String data = null;
                while ((data=br.readLine())!=null) {
                    if (data.startsWith("@")) {
                        sendMessageToOneClient(data);
                    } else {
                        sendMessageToAllClient(data);
                    }
                }

            }catch (IOException e){
                e.printStackTrace();
            }

        }
    }

    private void sendMessageToAllClient(String data){
        System.out.println("client msg:"+data);

        synchronized (writerList){
            Collection<PrintWriter> values = writerList.values();
            for (PrintWriter out : values) {
                        out.println(host+"说:"+data);
            }
        }
    }

    private void sendMessageToOneClient(String data){
        String msg = data.substring(data.lastIndexOf(":"));

        String nameUser = data.substring(1,data.lastIndexOf(":"));

        writerList.get(nameUser).println(nameUser + "说:" + msg);

    }



    //启动服务读取数据
    public void start(){
        while (true) {
        try {

                Socket socket = serverSocket.accept();
                System.out.println("客户端连上了,Cline:" + socket);
               new Thread(new ClientHandle(socket)).start();
//                Thread thread2 = new Thread(new WriteHandle(socket));

//                thread2.start();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}


    public static void main(String[] args){
        new MapThreadChatServer().start();

    }

}
